import React, { useEffect, useState } from 'react';
import dayjs from 'dayjs';
import {
  Button,
  Cascader,
  DatePicker,
  Form,
  Input,
  InputNumber,
  Radio,
  Select,
  Switch,
  TreeSelect,
} from 'antd';
import type { FormProps } from 'antd';
import {useDispatch} from 'react-redux'
import * as Async from '../app/store/splice'
type SizeType = Parameters<typeof Form>[0]['size'];
type FieldType = {
    username?: string;
    password?: string;
    remember?: string;
  };
const MyForm: React.FC = (props) => {
    let dispatch = useDispatch()
    const [form] = Form.useForm();
    useEffect(()=>{
        if(props.title=='编辑'){
            form.setFieldsValue(props.itemobj)
        }
    })
  const [componentSize, setComponentSize] = useState<SizeType | 'default'>('default');

  const onFormLayoutChange = ({ size }: { size: SizeType }) => {
    setComponentSize(size);
  };
  const onFinish: FormProps<FieldType>['onFinish'] = (values) => {
    console.log('Success:', values);
    if(props.title=='新增'){
        let obj={
            ...values,
        }
        dispatch(Async.async_addCW(obj))
    }else{
        let obj={
            ...props.itemobj,
            ...values,
           
        }
        dispatch(Async.async_editCW(obj))
    }
    props.onCancel()
  };

  return (
    <Form
    form={form}
      onFinish={onFinish}
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 14 }}
      layout="horizontal"
      initialValues={{ size: componentSize }}
      onValuesChange={onFormLayoutChange}
      size={componentSize as SizeType}
      style={{ maxWidth: 600 }}
    >
      <Form.Item label="财务标题" name="name">
        <Input />
      </Form.Item>
      <Form.Item label="关联客户" name="Customer">
        <Input />
      </Form.Item>
      <Form.Item label="商机阶段" name="stage">
        <Input />
      </Form.Item>
      <Form.Item label="报价金额" name="price">
        <Input />
      </Form.Item>
      <Form.Item>
        <Button htmlType='submit'>Button</Button>
      </Form.Item>
    </Form>
  );
};

export default MyForm;